package com.zsd.android.okhttplib.bean;

import android.text.TextUtils;

import java.io.IOException;
import java.net.Socket;

/**
 * 连接对象
 */
public class HttpConnection {

    private Socket mSocket;
    //连接对象最后使用时间
    private long lastUseTime;

    public HttpConnection() {

    }

    public HttpConnection(String host, int port) {
        try {
            mSocket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isConnectionAction(String host, int port) {
        if (mSocket == null) {
            return false;
        }
        //TextUtils是安卓的方法，如果在Java中测试，这样子不行
//        return TextUtils.equals(mSocket.getInetAddress().getHostName(), host) && mSocket.getPort() == port;
        return mSocket.getPort() == port && host.equals(mSocket.getInetAddress().getHostName());
    }

    public void closeSocket() {
        if (mSocket != null) {
            try {
                mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public long getLastUseTime() {
        return lastUseTime;
    }

    public void setLastUseTime(long lastUseTime) {
        this.lastUseTime = lastUseTime;
    }
}
